package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import fr.bordeaux1.planingdesetudiants.model.Etudiant;
import fr.bordeaux1.planingdesetudiants.model.Groupe;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class AddGroup extends Composite {
	private Text textNomGpe;
	private List listGroupe ;
	private IFenetrePrincipale fenetrePrincipale;
	private Combo listeUE;
	private List listEtudiant;
	private Label lblgroup;
	private Label lblEtudiantsUE;
	private Label lblEtudiantsG;

	public AddGroup(CompositUes arg0, int arg1, IFenetrePrincipale fenetrePrincipale) {
		super(arg0, arg1);
		this.fenetrePrincipale = fenetrePrincipale;
		this.setLayout(new FormLayout());

		Label nameE = new Label(this, SWT.NONE);
		FormData fd_nameE = new FormData();
		fd_nameE.left = new FormAttachment(20, 50);
		fd_nameE.top = new FormAttachment(0, 10);
		nameE.setLayoutData(fd_nameE);
		nameE.setText("Selectionner l'UE :");

		listeUE = new Combo(this, SWT.BORDER| SWT.READ_ONLY);
		fd_nameE.right = new FormAttachment(listeUE, 0,5);
		FormData fd_listeUE = new FormData();
		fd_listeUE.left = new FormAttachment(50, 10);
		fd_listeUE.right = new FormAttachment(100, -10);
		fd_listeUE.top = new FormAttachment(0,5);

		listeUE.setLayoutData(fd_listeUE);
		java.util.List<Ue> res;
		try {
			res = fenetrePrincipale.getDb().listerUes();

			for (Ue ue : res){
				listeUE.add(ue.getCode() + " " + ue.getLibelle());
				listeUE.setData(ue.getCode() + " " + ue.getLibelle(), ue);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		listeUE.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Ue ueSelected = (Ue) listeUE.getData(listeUE.getItem(listeUE.getSelectionIndex()));
				java.util.List<Etudiant> result;
				try {
					result = AddGroup.this.fenetrePrincipale.getDb().listerEtudiantsSansGroupeUe(ueSelected);
					listEtudiant.removeAll();
					for (Etudiant etu : result){
						listEtudiant.add(etu.getNom() + " " + etu.getPrenom());
						listEtudiant.setData(etu.getNom() + " " + etu.getPrenom(), etu);
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});

		lblgroup = new Label(this, SWT.NONE);
		lblgroup.setText("Nom du Groupe :");
		FormData fd_lblgroup = new FormData();
		fd_lblgroup.top = new FormAttachment(15, 21);
		fd_lblgroup.left = new FormAttachment(20, 0);
		fd_lblgroup.right = new FormAttachment(50, 0);
		lblgroup.setLayoutData( fd_lblgroup);

		textNomGpe = new Text(this, SWT.BORDER);
		FormData fd_textNomGpe = new FormData();
		fd_textNomGpe.left = new FormAttachment(lblgroup,10);
		fd_textNomGpe.top = new FormAttachment(15,20);
		fd_textNomGpe.right = new FormAttachment(100,-10);
		textNomGpe.setLayoutData(fd_textNomGpe);

		lblEtudiantsG = new Label(this, SWT.NONE);
		FormData fd_lblEtudiantsG = new FormData();
		fd_lblEtudiantsG.top = new FormAttachment(textNomGpe,20);
		fd_lblEtudiantsG.left = new FormAttachment(70, -25);
		lblEtudiantsG.setLayoutData(fd_lblEtudiantsG);
		lblEtudiantsG.setText("Etudiants inscrits à l'UE:");
		
		lblEtudiantsUE = new Label(this, SWT.NONE);
		FormData fd_lblEtudiantsUE = new FormData();
		fd_lblEtudiantsUE.top = new FormAttachment(textNomGpe,20);
		fd_lblEtudiantsUE.left = new FormAttachment(15, -30);
		lblEtudiantsUE.setLayoutData(fd_lblEtudiantsUE);
		lblEtudiantsUE.setText("Etudiants composant le groupe :");

		
		listGroupe = new List(this, SWT.BORDER| SWT.V_SCROLL|SWT.MULTI);
		FormData fd_listGroup = new FormData();
		fd_listGroup.top = new FormAttachment(lblEtudiantsUE,20);
		fd_listGroup.left = new FormAttachment(0,10);
		fd_listGroup.right = new FormAttachment(50,-10);
		fd_listGroup.bottom = new FormAttachment(90,0);
		listGroupe.setLayoutData(fd_listGroup);
		listGroupe.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (listGroupe.getSelectionIndex() != -1){
					listEtudiant.add(listGroupe.getItem(listGroupe.getSelectionIndex()));
					listGroupe.remove(listGroupe.getItem(listGroupe.getSelectionIndex()));
					layout();
				}
			}
		});


		listEtudiant = new List(this, SWT.BORDER| SWT.V_SCROLL);
		FormData fd_listEtudiant = new FormData();
		fd_listEtudiant.top = new FormAttachment(lblEtudiantsG,20);
		fd_listEtudiant.left = new FormAttachment(listGroupe,20);
		fd_listEtudiant.right = new FormAttachment(100,-10);
		fd_listEtudiant.bottom = new FormAttachment(90,0);
		listEtudiant.setLayoutData(fd_listEtudiant);
		listEtudiant.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (listEtudiant.getSelectionIndex() != -1){
					listGroupe.add(listEtudiant.getItem(listEtudiant.getSelectionIndex()));
					listEtudiant.remove(listEtudiant.getItem(listEtudiant.getSelectionIndex()));
					layout();
				}
			}
		});

		Button ok = new Button(this, SWT.NONE);
		FormData fd_ok = new FormData();
		fd_ok.top = new FormAttachment(listGroupe,20);
		fd_ok.left = new FormAttachment(50,-75);
		fd_ok.right = new FormAttachment(50,75);
		fd_ok.bottom = new FormAttachment(100,-5);
		ok.setLayoutData(fd_ok);
		ok.setText("Créer le groupe");
		ok.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				int index = listeUE.getSelectionIndex();
				if (index != -1) {
					Etudiant etud = new Etudiant();
					Ue ue = (Ue) listeUE.getData(listeUE.getItem(index));
	
					Groupe groupe = new Groupe();
					groupe.setCodeUe(ue.getCode());
					groupe.setNom(textNomGpe.getText());
					System.out.println("Groupe : "+ groupe);
					try {
						AddGroup.this.fenetrePrincipale.getDb().ajouterGroupe(groupe);
						for(int i=0; i < listGroupe.getItemCount();i++){
							String[] listItem = listGroupe.getItems();
							etud = (Etudiant) listEtudiant.getData(listItem[i]);
							java.util.List<Groupe> gpes = AddGroup.this.fenetrePrincipale.getDb().listerGroupesEtudiant(etud);
							for (Groupe gpe : gpes)
								if (gpe.getCodeUe().equalsIgnoreCase(ue.getCode()))
									AddGroup.this.fenetrePrincipale.getDb().retirerEtudiantDeGroupe(etud, gpe);
							AddGroup.this.fenetrePrincipale.getDb().ajouterEtudiantAuGroupe(etud, groupe);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					listeUE.deselectAll();
					listEtudiant.removeAll();
					listGroupe.removeAll();
					textNomGpe.setText("");
					AddGroup.this.fenetrePrincipale.modificationApportees();
					
					AddGroup.this.fenetrePrincipale.updateStatusBar("Nouveau groupe créé avec succès");
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
	}
}
